package ui;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import draft.Player;

public class PlayerList extends JPanel{
	
DefaultListModel model;
JList list;

	public PlayerList(ArrayList<Player> players){
		model = new DefaultListModel();
		for (int i = 0; i < players.size(); i++){
			((DefaultListModel) model).addElement(players.get(i));
		}
		list = new JList(model);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(list);
		setBackground(Color.GRAY);
		setLayout(new GridLayout(1, 1));
		setVisible(true);
	}
	
	public JList getList(){
		return list;
	}
	
	public DefaultListModel getModel(){
		return model;
	}
	
	
	/** Removes the player that is equal to support removing the player given a copy. */
	public void removePlayer (Player p) {
		DefaultListModel list = getModel();
		if (list.contains(p)) {
			list.removeElement(p);
			return;
		}
		for (int i=0; i<getModel().size(); ++i) {
			if (p.equals((Player)list.get(i))) {
				list.remove(i);
				return;
			}
		}
	}

}
